//
//  RestaurantCollectionViewCell.swift
//  FeastBooking
//
//  Created by Imon on 2017/7/6.
//  Copyright © 2017年 Imon. All rights reserved.
//

import UIKit

class RestaurantCollectionViewCell: UICollectionViewCell {
    
    let profilePic: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.layer.masksToBounds = true
        imageView.image = UIImage(named: "placeholder")
        return imageView
    }()
    
    let district: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.lightGray
        label.font = AppFont(size: 10)
        label.text = LanguageHelper.getString(key: "NO_RESULT")
        return label
    }()
    
    let name: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.gray
        label.textAlignment = .left
        label.font = AppFont(size: 15)
        label.numberOfLines = 0
        return label
    }()
    
    let price: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.red
        label.font = AppFont(size: 13)
        return label
    }()
    
    let priceSuffix: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.gray
        label.font = AppBoldFont(size: 10)
        label.text = "/\(LanguageHelper.getString(key: "TABLE"))"
        return label
    }()
    
    let comment: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.lightGray
        label.font = AppFont(size: 12)
        label.text = "0\(LanguageHelper.getString(key: "NUM_OF_REVIEW"))"
        return label
    }()
    
    let tables: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.lightGray
        label.font = AppFont(size: 12)
        label.text = "0-0\(LanguageHelper.getString(key: "TABLE"))"
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.layer.cornerRadius = 5
        self.layer.masksToBounds = true
        self.addSubview(profilePic)
        self.addSubview(district)
//        self.addSubview(distance)
        self.addSubview(name)
        self.addSubview(price)
        self.addSubview(priceSuffix)
        self.addSubview(comment)
        self.addSubview(tables)
    }
    
    func initValue(restaurant: RestaurantSummaryResponse) {
        self.name.text = restaurant.name
        if let priceRange = restaurant.priceRange {
            let lowP = Int(priceRange[0].rounded())
            let highP = Int(priceRange[1].rounded())
            self.price.text = "\(lowP)-\(highP)"
        }
        if let tableRange = restaurant.tableRange {
            self.tables.text = "\(tableRange[0])-\(tableRange[1])\(LanguageHelper.getString(key: "TABLE"))"
        }
        if let district = restaurant.districtOfAddress {
            self.district.text = district
        }
        if let nor = restaurant.numOfReviews {
            self.comment.text = "\(nor)\(LanguageHelper.getString(key: "NUM_OF_REVIEW"))"
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let textMaxSize = CGSize(width: bounds.width, height: 20)
        let districtWidth = TextSize.textSize(text: district.text!, font: AppFont(size: 10), maxSize: textMaxSize).width
//        let distanceWidth = TextSize.textSize(text: distance.text!, font: AppFont(size: 10), maxSize: textMaxSize).width
        let priceWidth = TextSize.textSize(text: price.text!, font: AppFont(size: 13), maxSize: textMaxSize).width
        let commentWidth = TextSize.textSize(text: comment.text!, font: AppFont(size: 12), maxSize: textMaxSize).width
        let tablesWidth = TextSize.textSize(text: tables.text!, font: AppFont(size: 12), maxSize: textMaxSize).width
        profilePic.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(0, 0, 0, bounds.width * 9 / 20))
        let distanceTopInset: CGFloat = 8
        district.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(distanceTopInset, bounds.width * 12 / 20, bounds.height - distanceTopInset - 10, bounds.width - bounds.width * 12 / 20 - districtWidth))
//        distance.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(distanceTopInset, bounds.width - 5 - distanceWidth, bounds.height - distanceTopInset - 10, 5))
        let nameMaxSize = CGSize(width: (bounds.width * 8 / 20) - 8 , height: 80)
        let nameSize = TextSize.textSize(text: name.text!, font: AppFont(size: 15), maxSize: nameMaxSize)
        let nameTopInset: CGFloat = distanceTopInset + 10 + 6
        name.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(nameTopInset, bounds.width * 12 / 20 - 2, bounds.height - nameTopInset - nameSize.height - 5, 8))
        price.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(bounds.height - 45, bounds.width * 12 / 20, 30, bounds.width * 8 / 20 - priceWidth))
        priceSuffix.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(bounds.height - 45, bounds.width * 12 / 20 + price.frame.width + 1, 30, 8))
        tables.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(bounds.height - 25, bounds.width * 12 / 20, 10, bounds.width * 8 / 20 - tablesWidth))
        comment.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(bounds.height - 25, bounds.width - commentWidth - 5, 10, 5))        
    }
}
